package com.gd.data.platform.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gd.data.platform.entity.DataResource;

/**
 * 数据资源Mapper
 * 操作MySQL中的数据资源元数据
 * 
 * @author gd-data-platform
 * @since 1.0.0
 */
@Mapper
public interface DataResourceMapper extends BaseMapper<DataResource> {

    /**
     * 根据资源类型查询资源列表
     */
    @Select("SELECT * FROM data_resources WHERE resource_type = #{resourceType} AND status = '启用'")
    List<DataResource> findByResourceType(@Param("resourceType") String resourceType);

    /**
     * 根据分类查询资源列表
     */
    @Select("SELECT * FROM data_resources WHERE category = #{category} AND status = '启用'")
    List<DataResource> findByCategory(@Param("category") String category);

    /**
     * 根据数据集ID查询资源列表
     */
    @Select("SELECT * FROM data_resources WHERE dataset_id = #{datasetId} AND status = '启用'")
    List<DataResource> findByDatasetId(@Param("datasetId") String datasetId);

    /**
     * 根据目录ID查询资源列表
     */
    @Select("SELECT * FROM data_resources WHERE directory_id = #{directoryId} AND status = '启用'")
    List<DataResource> findByDirectoryId(@Param("directoryId") String directoryId);

    /**
     * 根据资源ID查询资源信息
     */
    @Select("SELECT * FROM data_resources WHERE resource_id = #{resourceId}")
    DataResource findByResourceId(@Param("resourceId") String resourceId);

    /**
     * 搜索资源（按名称或描述）
     */
    @Select("SELECT * FROM data_resources WHERE " +
            "(resource_name LIKE CONCAT('%', #{keyword}, '%') OR description LIKE CONCAT('%', #{keyword}, '%')) " +
            "AND status = '启用'")
    List<DataResource> searchResources(@Param("keyword") String keyword);

    /**
     * 统计各类型资源数量
     */
    @Select("SELECT resource_type, COUNT(*) as count FROM data_resources WHERE status = '启用' GROUP BY resource_type")
    List<Object> countByResourceType();

    /**
     * 统计各分类资源数量
     */
    @Select("SELECT category, COUNT(*) as count FROM data_resources WHERE status = '启用' GROUP BY category")
    List<Object> countByCategory();
}
